#include "agricuturedroughtwaterloggingdialog.h"
#include "ui_agricuturedroughtwaterloggingdialog.h"
#include "rsalgorithmthread.h"
#include "rsagricuturedroughtwaterlogging.h"
#include <QFileDialog>

AgricutureDroughtWaterloggingDialog::AgricutureDroughtWaterloggingDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AgricutureDroughtWaterloggingDialog)
{
    ui->setupUi(this);

    ui->m_ProgressBar->setRange(0 , 1000);
    ui->m_ProgressBar->setValue(0);
}

AgricutureDroughtWaterloggingDialog::~AgricutureDroughtWaterloggingDialog()
{
    delete ui;
}

void AgricutureDroughtWaterloggingDialog::updateDlg(const int nValue, const QString strMsg)
{
    int nMax = ui->m_ProgressBar->maximum();
    int nMin = ui->m_ProgressBar->minimum();
    if ( nValue > nMax || nValue < nMin )
    {
        return;
    }

    ui->m_ProgressBar->setValue( nValue );

}

void AgricutureDroughtWaterloggingDialog::on_m_BtnInputImageFile_clicked()
{
    QString strFilePath = QFileDialog::getOpenFileName(this);
    if(QFile::exists(strFilePath))
    {
        ui->m_ComboBoxInputImageFile->addItem(strFilePath);
    }
}

void AgricutureDroughtWaterloggingDialog::on_m_BtnOutImageFile_clicked()
{
    QString strFilePath = QFileDialog::getSaveFileName(this);
    if(strFilePath != "")
    {
        ui->m_ComboBoxOutImageFile->addItem(strFilePath);
    }
}

void AgricutureDroughtWaterloggingDialog::on_m_BtnBox_accepted()
{
    ui->m_BtnBox->setEnabled(false);

    RSAlgorithmThread rsThread;
    connect(&rsThread, SIGNAL(updateDisPoseState(int , QString)),
            this, SLOT(updateDlg(int , QString)));

    //分配内存空间
    rsThread.m_pRSAlg = new RSAgricutureDroughtWaterlogging;
    if ( nullptr == rsThread.m_pRSAlg )
    {
        return;
    }

    //设置配置参数
    rsThread.m_pRSAlg->setInputImg(ui->m_ComboBoxInputImageFile->itemText(0).toUtf8().data());
    rsThread.m_pRSAlg->setOutputImg(ui->m_ComboBoxOutImageFile->itemText(0).toUtf8().data());
    static_cast<RSAgricutureDroughtWaterlogging*>(rsThread.m_pRSAlg)->setNewInputImg(ui->m_ComboBoxInputImageFile->itemText(0).toUtf8().data());
    static_cast<RSAgricutureDroughtWaterlogging*>(rsThread.m_pRSAlg)->setFlag(1);
    static_cast<RSAgricutureDroughtWaterlogging*>(rsThread.m_pRSAlg)->setOutputImgFormat(ui->m_ComboBoxOutImageFile->itemText(0).toUtf8().data());


    //启动线程运行
    rsThread.start();

    QEventLoop loop;
    connect(&rsThread, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec();

    ui->m_BtnBox->setEnabled(true);
}
